package com.walking.shopportal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author walking
 * 公众号：编程大道
 */
@RestController
public class BuyControllerV2 {
    /**
     * 引入ribbon，将负载均衡的能力赋予给RestTemplate
     */
    @Autowired
    @Qualifier("restTemplateV2")
    private RestTemplate restTemplate;

    /**
     * ribbon方式调用微服务，具有负载均衡的功能
     *
     * @param productId
     * @return
     */
    @GetMapping("/buyAutoLB/{productId}")
    public Map<String, Object> buyAutoLB(@PathVariable String productId) {
        // 调下单接口
        Map result = restTemplate.postForObject("http://order-service/order/create/{productId}", null,
                Map.class, productId);

        Map<String, Object> response = new HashMap<String, Object>();
        response.put("result", result);
        return response;
    }


}
